//
//  HomeViewController.m
//  foodlist
//
//  Created by HappyMilk 02 on 2017/8/15.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "HomeViewController.h"
#import "MJRequestServer.h"
#import "MJLocaStorage.h"
#import "LoginHeader.h"

#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
#import "SevenViewController.h"



@interface HomeViewController ()<UIScrollViewDelegate>

@property (nonatomic ,strong) UIScrollView *scrollview_cate;
@property (nonatomic ,strong) UIScrollView *scrollview_main;

@property (nonatomic ,strong) NSArray *cateArray;
@property (nonatomic ,strong) NSMutableArray *btnArray;
@property (nonatomic ,strong) UIView *view_xian;

@end

@implementation HomeViewController

- (NSArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
- (NSArray *)cateArray{
    if (!_cateArray) {
        _cateArray = @[@"川菜",@"湘菜",@"浙菜",@"粤菜",@"浙菜",@"东北菜",@"京菜"];
    }
    return _cateArray;
}
- (UIScrollView *)scrollview_cate{
    if (!_scrollview_cate) {
        _scrollview_cate = [[UIScrollView alloc] initWithFrame:(CGRect){0,64,__kwidth,35}];
        _scrollview_cate.contentSize = CGSizeMake(525, 0);
        _scrollview_cate.showsVerticalScrollIndicator = NO;
        _scrollview_cate.showsHorizontalScrollIndicator = NO;
        _scrollview_cate.delegate = self;
        [self.view addSubview:_scrollview_cate];
    }
    return _scrollview_cate;
}
- (UIScrollView *)scrollview_main{
    if (!_scrollview_main) {
        _scrollview_main = [[UIScrollView alloc] initWithFrame:(CGRect){0,99.5,__kwidth,__kheight-99.5}];
        _scrollview_main.contentSize = CGSizeMake(__kwidth*7, 0);
        _scrollview_main.showsVerticalScrollIndicator = NO;
        _scrollview_main.showsHorizontalScrollIndicator = NO;
        _scrollview_main.delegate = self;
        _scrollview_main.pagingEnabled = YES;
        _scrollview_main.bounces = NO;
        [self.view addSubview:_scrollview_main];
    }
    return _scrollview_main;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupCategroyView];
    
    [self setupMainScrollview];
    
    ADD_OBSERVER(self, @selector(appinfoload), NOTIFICATION_APP_INFO_LOADED, nil);
}
- (void)appinfoload{
    if (IS_PASS && IS_CHINESE) {
        CGPoint point = IS_IPAD?(CGPoint){0,__kheight-139}:(CGPoint){0,__kheight-99};
        GADBannerView *bannerview = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:point];
        bannerview.rootViewController = self;
        bannerview.adUnitID = AD_TEST_HF;//@"ca-app-pub-4634576436183649/7109315361";
        [self.tabBarController.view addSubview:bannerview];
        [bannerview loadRequest:[GADRequest request]];
        
    }
}

- (void)setupMainScrollview{
    
    VC_INIT_WITH_STORYBOARD(Main, OneViewController, one)
    [self addChildViewController:one];
    
    VC_INIT_WITH_STORYBOARD(Main, TwoViewController, two)
    [self addChildViewController:two];
    
    VC_INIT_WITH_STORYBOARD(Main, ThreeViewController, three)
    [self addChildViewController:three];
    
    VC_INIT_WITH_STORYBOARD(Main, FourViewController, four)
    [self addChildViewController:four];
    
    VC_INIT_WITH_STORYBOARD(Main, FiveViewController, five)
    [self addChildViewController:five];
    
    VC_INIT_WITH_STORYBOARD(Main, SixViewController, six)
    [self addChildViewController:six];
    
    VC_INIT_WITH_STORYBOARD(Main, SevenViewController, seven)
    [self addChildViewController:seven];
    
    one.view.frame = self.scrollview_main.bounds;
    [self.scrollview_main addSubview:one.view];
    
}
- (void)setupCategroyView{
    
    _view_xian = [[UIView alloc] initWithFrame:(CGRect){ 0, 33,75,2}];
    _view_xian.backgroundColor = HEXCOLOR(0xFD6A62);
    [self.scrollview_cate addSubview:_view_xian];
    
    UIView *xian = [[UIView alloc] initWithFrame:(CGRect){0,99,__kwidth,0.5}];
    xian.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:xian];
    
    for (int i=0; i<self.cateArray.count; i++) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){ i*75, 0, 75,35}];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [btn setTitle:self.cateArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTag:i];
        //[btn addTarget:self action:@selector(changeCategroyView:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollview_cate addSubview:btn];
        
        [self.btnArray addObject:btn];
        if (i == 0) {
            btn.selected = YES;
        }
    }
}
- (void)changeCategroyView:(UIButton *)btn{
    [self changeChildViewController:btn.tag];
    self.view_xian.frame = (CGRect){ btn.tag*75, 33, 75, 2};
}

#pragma mark ---- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat point_x = scrollView.contentOffset.x;
    //NSInteger index = point_x/__kwidth;
    if (scrollView == self.scrollview_main) {
        self.view_xian.frame = (CGRect){ point_x*75/__kwidth, 33, 75, 2};
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat point_x = scrollView.contentOffset.x;
    NSInteger index = point_x/__kwidth;
    if (scrollView == self.scrollview_main) {
        
        [self changeChildViewController:index];
    }
}
- (void)changeChildViewController:(NSInteger)index{
    
    UIViewController *vc = [self.childViewControllers objectAtIndex:index];
    vc.view.frame = self.scrollview_main.bounds;
    [self.scrollview_main addSubview:vc.view];
    if (!IS_IPAD) {
        [self.scrollview_cate setContentOffset:(CGPoint){ index*( 525-__kwidth)/7, self.scrollview_cate.contentOffset.y} animated:YES];
    }
    [self changeBtnSelectStyle:index];
}


- (void)changeBtnSelectStyle:(NSInteger)index{
    for (UIButton *btn in self.btnArray) {
        if (index == btn.tag) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
